package com.mallcloud.mall.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.constant.enums.mall.PointsCouponEnum;
import com.mallcloud.mall.mall.api.entity.vo.MallPointsCouponVO;
import com.mallcloud.mall.mall.mapper.MallPointsCouponMapper;
import com.mallcloud.mall.mall.service.MallPointsCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 积分券
 */
@Service
@RequiredArgsConstructor
public class MallPointsCouponServiceImpl implements MallPointsCouponService {

	private final MallPointsCouponMapper pointsCouponMapper;

	@Override
	public Page page(Page<MallPointsCouponVO> page, MallPointsCouponVO dto) {
		Page<MallPointsCouponVO> resultPage = null;
		if(dto.getPurpose().equals(PointsCouponEnum.PurposeEnum.PAY.getCode())){
			resultPage = pointsCouponMapper.pagePayCoupon(page, dto);
		}else if(dto.getPurpose().equals(PointsCouponEnum.PurposeEnum.FREE.getCode())){
			resultPage = pointsCouponMapper.pageFreeCoupon(page, dto);
		}else if(dto.getPurpose().equals(PointsCouponEnum.PurposeEnum.REDEEM.getCode())){
			resultPage = pointsCouponMapper.pageRedeemCoupon(page, dto);
		}
		resultPage.getRecords().forEach(x -> x.setTempId(String.format("%s,%s", x.getType(), x.getScheduleId())));
		return resultPage;
	}

	@Override
	public MallPointsCouponVO getInfo(MallPointsCouponVO dto) {
		MallPointsCouponVO result = null;
		if(dto.getPurpose().equals(PointsCouponEnum.PurposeEnum.PAY.getCode())){

			result = pointsCouponMapper.getPayInfo(dto);
		}else if(dto.getPurpose().equals(PointsCouponEnum.PurposeEnum.FREE.getCode())){

			result = pointsCouponMapper.getFreeInfo(dto);
		}else if(dto.getPurpose().equals(PointsCouponEnum.PurposeEnum.REDEEM.getCode())){

			result = pointsCouponMapper.getRedeemInfo(dto);
		}
		result.setTempId(String.format("%s,%s", result.getType(), result.getScheduleId()));
		return result;
	}
}
